package com.ruoyi.eqmonitor.smdtv.controller;

import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.ruoyi.eqmonitor.smdtv.domain.Smdtv11;
import com.ruoyi.eqmonitor.smdtv.domain.SmdtvSewagePlant;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvSewagePlantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Api(tags = "污水厂")
@RestController
@RequestMapping("/plant")
public class SmdtvSewagePlantController extends BaseController{
    
    @Autowired
    private ISmdtvSewagePlantService smdtvSewagePlantService;

    /**
     * 查询污水厂列表
     */
    @GetMapping("/list")
    public TableDataInfo<?> list(SmdtvSewagePlant smdtvSewagePlant){
        startPage();
        List<SmdtvSewagePlant> list = smdtvSewagePlantService.selectSmdtvSewagePlantList(smdtvSewagePlant);
        return getDataTable(list);
    }

    /**
     * 获取全部污水厂
     */
    @GetMapping("/all")
    public AjaxResult all(SmdtvSewagePlant smdtvSewagePlant){
        return AjaxResult.success(smdtvSewagePlantService.selectSmdtvSewagePlantList(smdtvSewagePlant));
    }

    /**
     * 根据id查询污水厂
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(smdtvSewagePlantService.selectSmdtvSewagePlantById(id));
    }

    /**
     * 新增污水厂
     */
    @Log(title = "污水厂", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmdtvSewagePlant smdtvSewagePlant){
        smdtvSewagePlant.setCreateTime(new Date());
        return toAjax(smdtvSewagePlantService.save(smdtvSewagePlant));
    }

    /**
     * 修改污水厂
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmdtvSewagePlant smdtvSewagePlant){
        return toAjax(smdtvSewagePlantService.updateById(smdtvSewagePlant));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "污水厂", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(smdtvSewagePlantService.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出污水厂列表
     */
    @Log(title = "污水厂", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmdtvSewagePlant smdtvSewagePlant){
        List<SmdtvSewagePlant> list = smdtvSewagePlantService.selectSmdtvSewagePlantList(smdtvSewagePlant);
        ExcelUtil<SmdtvSewagePlant> util = new ExcelUtil<SmdtvSewagePlant>(SmdtvSewagePlant.class);
        util.exportExcel(response, list, "污水厂数据");
    }

    @ApiOperation("数据导入")
    @PostMapping("/toLeadJSON")
    public boolean toLeadJSON(HttpServletRequest request){
        try {
            MultipartHttpServletRequest requestStr1 = (MultipartHttpServletRequest) request;
            Iterator<String> fileNames = requestStr1.getFileNames();
            while (fileNames.hasNext()) {
                String next = fileNames.next();
                List<MultipartFile> files = requestStr1.getFiles(next);
                if (files.size() > 0) {
                    Iterator<MultipartFile> iterator = files.iterator();
                    while (iterator.hasNext()) {
                        MultipartFile next1 = iterator.next();//获取到文件
                        InputStream inputStream = next1.getInputStream();
                        return smdtvSewagePlantService.toLeadJSON(inputStream);

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
}
